package com.hust.chatvedio.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.appcompat.view.menu.MenuBuilder;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;

import com.hust.chatvedio.MenuActivity;
import com.hust.chatvedio.R;
import com.hust.chatvedio.AddFriendActivity;

import java.lang.reflect.Method;


public class DiscoverFragment extends Fragment {

    private Toolbar mToolbar;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_discovery, container, false);
        initView(view);
        setHasOptionsMenu(true);
        return view;
    }

    private void initView(View view) {
        mToolbar = (Toolbar) view.findViewById(R.id.toolbar);
        mToolbar.setOverflowIcon(getResources().getDrawable(R.drawable.add));
        MenuActivity menuActivity = (MenuActivity) getActivity();
        menuActivity.setSupportActionBar(mToolbar);
        mToolbar.setTitle("发现");
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.main, menu);
        if (menu != null) {
            if (menu.getClass() == MenuBuilder.class) {
                try {
                    Method m = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
                    m.setAccessible(true);
                    m.invoke(menu, true);
                } catch (Exception e) {
                }
            }
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.search_item:
                Toast.makeText(getContext(), "开发中，耐心等待哦~", Toast.LENGTH_SHORT).show();
                break;
            case R.id.chat_item:
                Toast.makeText(getContext(), "开发中，耐心等待哦~", Toast.LENGTH_SHORT).show();
                break;
            case R.id.addfriend_item:
                Intent intent = new Intent(getActivity(), AddFriendActivity.class);
                startActivity(intent);
                break;
            case R.id.addGroup_item:
                Toast.makeText(getContext(), "开发中，耐心等待哦~", Toast.LENGTH_SHORT).show();
                break;
            case R.id.pay_item:
                Toast.makeText(getContext(), "开发中，耐心等待哦~", Toast.LENGTH_SHORT).show();
                break;
            case R.id.help_item:
                Toast.makeText(getContext(), "开发中，耐心等待哦~", Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}